package com.medium.house.utils.excel;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ReaderExcel {

    private HSSFWorkbook book;
    private XSSFWorkbook xbook;
    private Sheet sheet;
    private boolean is2007;

    public ReaderExcel(File file, boolean is2007) {
        try {
            if (!is2007) {
                InputStream in = new FileInputStream(file);
                book = new HSSFWorkbook(in);
                sheet = book.getSheetAt(0);
            } else {
                InputStream in = new FileInputStream(file);
                xbook = new XSSFWorkbook(in);
                sheet = xbook.getSheetAt(0);
            }
            this.is2007 = is2007;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public ReaderExcel(InputStream in, boolean is2007) {
        try {
            if (!is2007) {
                book = new HSSFWorkbook(in);
                sheet = book.getSheetAt(0);
            } else {
                xbook = new XSSFWorkbook(in);
                sheet = xbook.getSheetAt(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param cell
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public List readExcelToList(Class c, String[] codes, int startRow, int startCol) throws InstantiationException, IllegalAccessException {
        List list = new ArrayList();

        Row row = sheet.getRow(startRow);
        // 得到总行数
        int rowNum = sheet.getLastRowNum();
        int columnNum = row.getPhysicalNumberOfCells();
        if (rowNum > startRow && columnNum > startCol) {
            for (int i = startRow; i <= rowNum; i++) {
                Row tempRow = sheet.getRow(i);
                Object obj = c.newInstance();
                int no = 0;
                for (int j = startCol; j < columnNum; j++) {
                    Cell tempCell = tempRow.getCell(j);
                    if (tempCell != null) {
                        String code = codes[no];
                        String setMethodName = MethodTool.getMethodNameBySet(code);
                        Object value = getValueToStr(tempCell);
                        try {
                            Field field = c.getDeclaredField(code);
                            MethodTool.setValue(obj, field, setMethodName, value);
                        } catch (Exception e) {
                            e.printStackTrace();
                            System.out.println("set method value error method is " + setMethodName);
                        }
                    }
                }
                list.add(obj);
            }
        }
        return list;
    }

    /**
     * 获取单元格数据内容为字符串类型的数据
     *
     * @param cell Excel单元格
     * @return String 单元格数据内容
     */
    private String getValueToStr(Cell cell) {
        String str = null;
        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                str = cell.getStringCellValue();
                break;
            case Cell.CELL_TYPE_NUMERIC:
                str = String.valueOf(cell.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                str = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_BLANK:
                break;
            default:
                break;
        }
        return str;
    }



	/*
     * public static void main(String[] args) { File file = new
	 * File("G:\\Test.xlsx"); ReaderExcel readExcel = new ReaderExcel(file,
	 * true); String[] fields = new String[] { "t1", "t2", "t3", "t4", "t5" };
	 * try { List<TestVo> list = readExcel.readExcelToList(TestVo.class, fields,
	 * 0, 0); System.out.println(list.size()); } catch (InstantiationException
	 * e) { e.printStackTrace(); } catch (IllegalAccessException e) {
	 * e.printStackTrace(); } }
	 */
}
